package com.ruoyi.project.dict.erya.controller;

import com.ruoyi.common.utils.EryaHelper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.erya.domain.Erya;
import com.ruoyi.project.dict.erya.service.IEryaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 尔雅注疏Controller
 *
 * @author shixiangwen
 * @date 2021-08-16
 */
@Controller
@RequestMapping("/dict/erya")
public class EryaController extends BaseController {
    private String prefix = "dict/erya";

    @Autowired
    private IEryaService eryaService;

    @RequiresPermissions("dict:erya:view")
    @GetMapping()
    public String erya() {
        return prefix + "/erya";
    }

    /**
     * 查询尔雅注疏列表
     */
    @RequiresPermissions("dict:erya:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Erya erya) {
        startPage();
        List<Erya> list = eryaService.selectEryaList(erya);
        return getDataTable(list);
    }

    /**
     * 导出尔雅注疏列表
     */
    @RequiresPermissions("dict:erya:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Erya erya) {
        List<Erya> list = eryaService.selectEryaList(erya);
        ExcelUtil<Erya> util = new ExcelUtil<Erya>(Erya.class);
        return util.exportExcel(list, "erya");
    }

    /**
     * 新增尔雅注疏
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存尔雅注疏
     */
    @RequiresPermissions("dict:erya:add")
    @Log(title = "尔雅注疏", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Erya erya) {
        return toAjax(eryaService.insertErya(erya));
    }

    /**
     * 修改尔雅注疏
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Erya erya = eryaService.selectEryaById(id);
        mmap.put("erya", erya);
        return prefix + "/edit";
    }

    @GetMapping("/preview/{id}")
    public String preview(@PathVariable("id") Long id, ModelMap mmap) {
        Erya erya = eryaService.selectEryaById(id);

        String content = EryaHelper.convert2html(erya.getClassify(), erya.getDetail(), erya.getZhushu());
        mmap.put("content", content);
        return prefix + "/preview";
    }

    /**
     * 修改保存尔雅注疏
     */
    @RequiresPermissions("dict:erya:edit")
    @Log(title = "尔雅注疏", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Erya erya) {
        return toAjax(eryaService.updateErya(erya));
    }

    /**
     * 删除尔雅注疏
     */
    @RequiresPermissions("dict:erya:remove")
    @Log(title = "尔雅注疏", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(eryaService.deleteEryaByIds(ids));
    }
}
